<template>
  <div>
    <h1>{{ id ? '编辑' : '新建' }}英雄</h1>
    <el-form label-width="100px" @submit.native.prevent="save" style="width:500px">
      <el-tabs type="border-card" v-model="activeName" style="width:700px">
        <el-tab-pane label="基础信息" name="base">
          <el-form-item label="英雄名称">
            <el-input v-model="model.name" class="input"></el-input>
          </el-form-item>
          <el-form-item label="英雄称号">
            <el-input v-model="model.title" class="input"></el-input>
          </el-form-item>
          <el-form-item label="英雄头像">
            <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeader()" :show-file-list="false"
              :on-success="handleAvatarSuccess">
              <img v-if="model.avatar" :src="model.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="banner">
            <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeader()" :show-file-list="false"
              :on-success=" res => $set(model,'banner',res.url)">
              <img v-if="model.banner" :src="model.banner" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="英雄类型">
            <el-select v-model="model.categories" multiple>
              <el-option v-for="category in categories" :key="category._id" :value="category._id"
                :label="category.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度">
            <el-rate style="margin-top:0.6rem" show-score :max="9" v-model="model.scores.difficulty"></el-rate>
          </el-form-item>
          <el-form-item label="技能">
            <el-rate style="margin-top:0.6rem" show-score :max="9" v-model="model.scores.skill"></el-rate>
          </el-form-item>
          <el-form-item label="攻击">
            <el-rate style="margin-top:0.6rem" show-score :max="9" v-model="model.scores.attack"></el-rate>
          </el-form-item>
          <el-form-item label="生存">
            <el-rate style="margin-top:0.6rem" show-score :max="9" v-model="model.scores.survival"></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple>
              <el-option v-for="item in items" :key="item._id" :value="item._id" :label="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple>
              <el-option v-for="item in items" :key="item._id" :value="item._id" :label="item.name">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input v-model="model.usageTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input v-model="model.battleTips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input v-model="model.teamIdea" type="textarea"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能管理" name="skills">
          <el-button type="text" @click="model.skills.push({})"><i class="el-icon-plus"></i>添加技能</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :span="12" v-for="(skill, i) in model.skills" :key="i"
              style="margin-bottom:10px;border-bottom:1px solid #ccc;">
              <el-form-item label="名称">
                <el-input v-model="skill.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload class="avatar-uploader" :action="uploadUrl" :headers="getAuthHeader()"
                  :show-file-list="false" :on-success="res => $set(skill, 'icon', res.url)">
                  <img v-if="skill.icon" :src="skill.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="冷却值">
                <el-input v-model="skill.cd"></el-input>
              </el-form-item>
              <el-form-item label="消耗">
                <el-input v-model="skill.consume"></el-input>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="skill.description" type="textarea"></el-input>
              </el-form-item>
              <el-button style="margin-left:100px" type="danger" @click="model.skills.splice(i,1)">删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="英雄关系" name="relation">
          <el-button type="text" @click="model.partner.push({})"><i class="el-icon-plus"></i>添加关系</el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col v-for="(item,i) in model.partner" :key="i" style="margin-bottom:20px">
              <el-form-item label="英雄">
                <el-select v-model="item.hero" filterable>
                  <el-option v-for="hero in heroes" :key="hero._id" :value="hero._id" :label="hero.name">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-button style="margin-left:100px;" type="danger" @click="model.partner.splice(i,1)">
                删除</el-button>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" native-type="submit" style="margin-top:20px">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {}
  },
  data () {
    return {
      model: {
        scores: {},
        skills: [],
        partner: []
      },
      categories: [],
      items: [],
      heroes: [],
      activeName: 'base'
    }
  },
  methods: {
    async save () {
      var res
      if (this.id) {
        res = await this.$http.put(`rest/heroes/${this.id}`, this.model)
      } else {
        res = await this.$http.post('rest/heroes', this.model)
      }
      // this.$router.push('/heroes/list')
      this.$message({
        type: 'success',
        message: '操作成功'
      })
      console.log(res)
    },
    async fetch () {
      const res = await this.$http.get(`rest/heroes/${this.id}`)
      this.model = Object.assign({}, this.model, res.data)
    },
    async fetchCategories () {
      const res = await this.$http.get('rest/categories')
      const newData = res.data
        .filter(item => item.parent)
        .filter(item => item.parent._id === '5f41e8ed0fd93e37e0e1ca66')
      this.categories = newData
    },
    async fetchItems () {
      const res = await this.$http.get('rest/items')
      this.items = res.data
    },
    async fetchHeroes () {
      const res = await this.$http.get('rest/heroes')
      this.heroes = res.data
    },
    handleAvatarSuccess (res) {
      this.$set(this.model, 'avatar', res.url)
    }
  },
  created () {
    this.fetchItems()
    this.fetchHeroes()
    this.fetchCategories()
    this.id && this.fetch()
  }
}
</script>
